package com.beaven.imclient.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.beaven.imclient.R;
import com.beaven.imclient.base.BaseActivity;
import com.beaven.imclient.fragment.FriendFragment;
import com.beaven.imclient.fragment.MessageFragment;
import com.beaven.imclient.fragment.MineFragment;
import com.beaven.imclient.utils.ImageUtil;
import com.beaven.imclient.widget.DeviceBottomPopup;

public class MainActivity extends BaseActivity implements View.OnClickListener {

  public static void start(Context context) {
    context.startActivity(new Intent(context, MainActivity.class));
  }

  private TextView text_content_center;
  private ImageView image_arrow_right;
  private TextView text_message_main;
  private TextView text_friend_main;
  private TextView text_mine_main;
  private DeviceBottomPopup deviceBottomPopup;
  private Fragment currentFragment;

  private Fragment[] fragments =
      { MessageFragment.newInstance(), FriendFragment.newInstance(), MineFragment.newInstance() };

  @Override public int getLayoutId() {
    return R.layout.activity_main;
  }

  @Override protected void initWidget() {
    super.initWidget();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setBackgroundColor(getResources().getColor(R.color.primary));
    View view_status_bar = findViewById(R.id.view_status_bar);
    view_status_bar.setBackgroundColor(getResources().getColor(R.color.primary));
    text_content_center = (TextView) findViewById(R.id.text_content_center);
    image_arrow_right = (ImageView) findViewById(R.id.image_arrow_right);
    text_message_main = (TextView) findViewById(R.id.text_message_main);
    text_friend_main = (TextView) findViewById(R.id.text_friend_main);
    text_mine_main = (TextView) findViewById(R.id.text_mine_main);

    text_message_main.setOnClickListener(this);
    text_friend_main.setOnClickListener(this);
    text_mine_main.setOnClickListener(this);
    image_arrow_right.setOnClickListener(this);

    toggleFragment(fragments[0]);
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.text_message_main:
        toggleFragment(fragments[0]);
        break;
      case R.id.text_friend_main:
        toggleFragment(fragments[1]);
        break;
      case R.id.text_mine_main:
        toggleFragment(fragments[2]);
        break;
      case R.id.image_arrow_right:
        showBottomPup();
        break;
      case R.id.text_user_add:
        deviceBottomPopup.dismiss();
        SettingActivity.start(MainActivity.this, SettingActivity.TYPE_SEARCH_NAME);
        break;
      case R.id.text_group_create:
        deviceBottomPopup.dismiss();
        SettingActivity.start(MainActivity.this, SettingActivity.TYPE_CREATE_GROUP);
        break;
      case R.id.text_dismiss:
        deviceBottomPopup.dismiss();
        break;
      default:
        break;
    }
  }

  private void toggleFragment(Fragment fragment) {
    if (fragment instanceof MessageFragment) {
      text_content_center.setText(getString(R.string.message));
      image_arrow_right.setBackground(getResources().getDrawable(R.drawable.image_add));
      setSelect(text_message_main);
      setUnSelect(text_friend_main);
      setUnSelect(text_mine_main);
    } else if (fragment instanceof FriendFragment) {
      text_content_center.setText(getString(R.string.friend_list));
      image_arrow_right.setBackground(getResources().getDrawable(R.drawable.image_add));
      setUnSelect(text_message_main);
      setSelect(text_friend_main);
      setUnSelect(text_mine_main);
    } else if (fragment instanceof MineFragment) {
      text_content_center.setText(getString(R.string.mine));
      image_arrow_right.setBackground(null);
      setUnSelect(text_message_main);
      setUnSelect(text_friend_main);
      setSelect(text_mine_main);
    }

    switchFragment(fragment);
  }

  private void switchFragment(Fragment fragment) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transition = manager.beginTransaction();
    if (currentFragment == null) {
      transition.add(R.id.layout_main_frame, fragment);
    } else if (fragment != currentFragment) {
      if (!fragment.isAdded()) {
        transition.hide(currentFragment).add(R.id.layout_main_frame, fragment);
      } else {
        transition.hide(currentFragment).show(fragment);
      }
    }
    transition.commit();
    currentFragment = fragment;
  }

  private void setSelect(TextView textView) {
    textView.setTextColor(getResources().getColor(R.color.primary));
    Drawable[] drawables = textView.getCompoundDrawables();
    Drawable drawable =
        ImageUtil.tintDrawable(drawables[1], getResources().getColor(R.color.primary));
    textView.setCompoundDrawables(null, drawable, null, null);
  }

  private void setUnSelect(TextView textView) {
    textView.setTextColor(getResources().getColor(R.color.gray));
    Drawable[] drawables = textView.getCompoundDrawables();
    Drawable drawable = ImageUtil.tintDrawable(drawables[1], getResources().getColor(R.color.gray));
    textView.setCompoundDrawables(null, drawable, null, null);
  }

  private void showBottomPup() {
    View view = LayoutInflater.from(this).inflate(R.layout.popu_main_add, null);
    deviceBottomPopup = new DeviceBottomPopup(this, view, image_arrow_right,
        new ColorDrawable((getResources().getColor(android.R.color.transparent))),
        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    view.findViewById(R.id.text_user_add).setOnClickListener(this);
    view.findViewById(R.id.text_group_create).setOnClickListener(this);
    view.findViewById(R.id.text_dismiss).setOnClickListener(this);
  }
}
